<template>
	<view class="content">
		<view class="cu-bar search bg-white">
			<view class="search-form ">
				<text class="text-red">已选中：</text>
				<input type="text" v-model="keyword" />
			</view>
			<view class="action">
				<button class="cu-btn bg-green shadow-blur round" @tap="handelSearch">确定</button>
				<!-- <button class="cu-btn bg-blue shadow round" @tap="openVideo">
					<span class="cuIcon-recordfill"></span>
				</button> -->
			</view>
		</view>
		<Tree :dataList="list" @lastClick="col"></Tree>
	</view>
</template>

<script>
	import Tree from "@/components/Tree/tree.vue";
	import {
		mapState,
		mapMutations
	} from "vuex";
	import api from "../../common/api.js";
	import util from "../../common/util.js";
	
	var testModule = uni.requireNativePlugin("TestModule")
	
	let _self,
		searchUrl,
		urlParams,
		types,
		queryJson = {};

	let requestData = {
		category: "",
		keyword: "",
		VNAME: "",
		comboboxId: "",
		queryJson: "",
		organizeId: "",
		QueryType: 0,
		userId: "",
		account: ""
	};

	export default {
		computed: mapState(["userInfo"]),
		components: {
			Tree
		},
		data() {
			return {
				list: [],
				storeList: [],
				keyword: '',
				keywordID: '',
				keywordR: 0
			};
		},
		onBackPress() {
			uni.hideKeyboard();
		},

		onLoad(p) {
			_self = this;
			requestData.category = "BD_STORE";
			this.initData(p);
			uni.$on("lastClick", res => {
				this.keyword = res.item.t
				this.keywordID = res.item.id
				this.keywordR = res.item.r;
				// console.log(  888, res.item.c.length);
				if (res.item.r == 5 || !res.item.o) return;
				if (res.item.o && res.item.c.length == 0) {
					_self.addCDta(res.item);
				}
			});
		},
		onUnload() {
			uni.$off('lastClick')
		},
		// onHide() {
		// 	uni.$off('lastClick')
		// },
		methods: {
			initData(p) {
				queryJson = {};
				requestData.queryJson = JSON.stringify(queryJson);
				requestData.organizeId = p.PK_ORG ? p.PK_ORG : _self.userInfo.ORGANIZEID;
				requestData.QueryType = 0;
				requestData.userId = _self.userInfo.UserId;
				requestData.account = _self.userInfo.Account;
				// console.log(requestData);
				this.getList();
			},
			handelSearch() {
				// console.log(111);
				let place = {
					name: this.keyword,
					id: this.keywordID,
					R: this.keywordR
				};
				// console.log(place);
				uni.$emit('updataPlace', place)
				uni.navigateBack();
			},
			getList(pArr) {
				let URL = api.apiPublic.searchUrl;
				// console.log(URL);
				console.log(requestData);
				this.axios({
					url: URL,
					data: requestData,
					isLoading: true
				}).then(res => {
					let {
						IsSuccess,
						Result
					} = res;
					// console.log(Result);
					let arr = Result.map((i, index) => {
						let pid = pArr ? [...pArr, index] : [index];
						let a = {
							id: i.BILLNO,
							t: i.FULLVNAME || i.VNAME,
							vd: i.VDEF1 ,
							gdt: i.GDTYPE ,
							p: pid,
							r: pid.length,
							o: false,
							c: []
						};
						return a;
					});
					if (pArr) {
						switch (pArr.length) {
							case 1:
								this.list[pArr[0]].c = arr;
								break;
							case 2:
								this.list[pArr[0]].c[pArr[1]] ?
									(this.list[pArr[0]].c[pArr[1]].c = arr) : [];
								break;
							case 3:
								this.list[pArr[0]].c[pArr[1]].c[pArr[2]].c = arr;
								break;
							case 4:
								this.list[pArr[0]].c[pArr[1]].c[pArr[2]].c[pArr[3]].c = arr;
								break;
							case 5:
								this.list[pArr[0]].c[pArr[1]].c[pArr[2]].c[pArr[2]].c[
									pArr[4]
								].c = arr;
								break;
						}
					} else {
						this.list = [...arr];
					}
				});
			},
			//点击最后一级时触发该事件
			mapdata(data) {
				let arr = data.map((i, index) => {
					let pid = [...index];
					let a = {
						id: i.BILLNO,
						t: i.VNAME, // title
						vd: i.VDEF1 ,
						gdt: i.GDTYPE ,
						p: pid, // 父节点下标数组
						r: pid.length, //  层级
						c: [] // 子节点数组
					};
					return a;
				});
				this.list = arr;
			},
			col() {

			},
			addCDta(item) {
				requestData.category = "BD_RACK";
				console.log(55, item);
				queryJson = {}
				switch (item.r) {
					case 1:
						queryJson.PK_STORE = item.id;
						queryJson.GDTYPE = 0;
						break;
					case 2:
					case 3:
					case 4:
					case 5:
						queryJson.PK_STORE = "";
						queryJson.PK_PARENT = item.id;
						queryJson.GDTYPE = item.r * 1 - 1;
						break;
					default:
						queryJson.PK_STORE = item.id;
						queryJson.GDTYPE = 0;
						break;
				}
				requestData.queryJson = JSON.stringify(queryJson);
				console.log(66, requestData);
				this.getList(item.p);
			},

			openVideo() {
				// name: this.keyword,
				// id: this.keywordID,
				// R: this.keywordR
				let videoPar = {
					zbOrHwBillNo: this.keywordID || 'WH000000000000000369' ,
					type: '1' ,
				}
				this.axios({
					url: '/API/APPCommon/GetSysBoadCamera',
					data: videoPar,
					isLoading: true
				}).then(res => {
					let {
						IsSuccess,
						Result
					} = res;
					if(!Result.length) return this.$utils.showTips('监控未配置，请联系管理员')
					this.gotoNativePage(Result[0])
					console.log(Result);
				})
			},
			
			gotoNativePage(data) {
				// id: this.keywordID,
				console.log(getApp().globalData.Token ,
					getApp().globalData.BaseURL ,
					data.VIDEOTYPE || '3', // 查看类型  1 云台， 2轨迹， 3盘点数量
					data.CameraNo || '', // 摄像头IP
					data.LoginName || '',// 摄像头密码账户
					data.PassWord || '', // 摄像头密码
					data.Port || '', // 摄像头端口
					data.Channel || '0', // 摄像头通道号
					data.VDEF4 || '0', // 录像机IP 
					data.VDEF5 || '', // 录像机账户
					data.VDEF6 || '', // 录像机密码
					data.VDEF7 || '0', // 录像端口
					data.VDEF11 || '0', // 录像机通道号
					'',
					'',
					this.keyword ||'',
					'',
					'');
				// let newParams = '';
				testModule.gotoNativePage(
					getApp().globalData.Token ,
					getApp().globalData.BaseURL ,
					data.VIDEOTYPE || '3', // 查看类型  1 云台， 2轨迹， 3盘点数量
					data.CameraNo || '', // 摄像头IP
					data.LoginName || '',// 摄像头密码账户
					data.PassWord || '', // 摄像头密码
					data.Port || '', // 摄像头端口
					data.Channel || '0', // 摄像头通道号
					data.VDEF4 || '0', // 录像机IP 
					data.VDEF5 || '', // 录像机账户
					data.VDEF6 || '', // 录像机密码
					data.VDEF7 || '0', // 录像端口
					data.VDEF11 || '0', // 录像机通道号
					'',
					'',
					this.keyword ||'',
					'',
					''
				);
			},
			
			
			
		}
	};
</script>

<style lang="scss">
	page {
		background-color: #ffffff;
	}
</style>